from word import *

class document:
    
    def __init__(self, loc = "", name = "", type = ""):
        """
        loc - path to the document
        name - a string referring to the name of the document
        type - a string such as "html", "ebk", "mail" etc. 
        word_objects - map string to word objects
        """
        self.word_objects = {}
        self.location = loc
        self.name = name
        self.type = type
        self.title = []
        self.authors = []
        self.doclength = 1
        self.input_title = ""
        self.input_authors = ""
    
    def __eq__(self, other_doc):
        """
        Two documents are considered equal
        iff they have the same name, file type, and same location list
        """
        return ((self.name == other_doc.name) and (self.type == other_doc.type) and (self.location == other_doc.location))
    
    def __hash__(self):
        return hash(self.name)+hash(self.type)
    
    def getFileName(self):
        """
        Return the file name with its type, i.e. "example1.ebk"
        """
        return self.name + self.type

    def getLocation(self):
        """
        Return location list
        """
        return self.location+self.getFileName()
    
    def addWord(self, string, location):
        """
        Add a new word to the document
        
        Update the word's location as well as its count.
        """
       # print string,location
        self.doclength += 1
        if self.containsWord(string):
            self.word_objects[string].updateWord(location)
        else:
            self.word_objects[string] = word(string)
            self.word_objects[string].updateWord(location)
    
    def indexDoc(self,stem_list):
        """
        Add all the words in the stem_list into document
        """
        for location in range(len(stem_list)):
            self.addWord(stem_list[location], location)
            
    def containsWord(self, string):
        """
        Returns true if the string is already in document's list of words
        """
        return string in self.word_objects.keys()
    
    def getWordCount(self, word):
        """
        Return the count of the word
        """
        if not self.containsWord(word):
            return 0
        return self.word_objects[word].count
    
    def getWordLocations(self, word):
        """
        Return location list of the word
        """
        if not self.containsWord(word):
            return []
        return self.word_objects[word].locations
    
    def getAvgLoc(self, string):
        """
        Return the average of all the location
        """
        if not self.containsWord(string):
            return 0
        return self.word_objects[string].getAvgLoc()